--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Node / ReticulumProjects / NomadNet.git / files / nomadnet / vendor / additional_urwid_widgets / assisting_modules / useful_functions.py
Displaying Raw • Download
nomadnet/vendor/additional_urwid_widgets/assisting_modules/useful_functions.py 0.2.1 (35f624ee) Text, 1.72 KB
T8b949e#! /usr/bin/env python3
T8b949e# -*- coding: utf-8 -*-
T8b949e"""A non-thematic collection of useful functions."""
Tff7b72def Td2a8ffrecursively_replaceTb4b4b4(Te6edf3originalTb4b4b4, Te6edf3replacementsTb4b4b4, Te6edf3include_original_keysTff7b72=Tff7b72FalseTb4b4b4)Tb4b4b4:
T8b949e"""Clones an iterable and recursively replaces specific values."""
T8b949e# If this function would be called recursively, the parameters 'replacements' and 'include_original_keys' would have to be
T8b949e# passed each time. Therefore, a helper function with a reduced parameter list is used for the recursion, which nevertheless
T8b949e# can access the said parameters.
Tff7b72def Td2a8ff_recursion_helperTb4b4b4(Te6edf3objTb4b4b4)Tb4b4b4:
T8b949e#Determine if the object should be replaced. If it is not hashable, the search will throw a TypeError.
Tff7b72tryTb4b4b4:
Tff7b72if Te6edf3obj Tff7b72in Te6edf3replacementsTb4b4b4:
Tff7b72return Te6edf3replacementsTb4b4b4[Te6edf3objTb4b4b4]
Tff7b72except Tf85149TypeErrorTb4b4b4:
Tff7b72pass
T8b949e# An iterable is recursively processed depending on its class.
Tff7b72if Tffa657hasattrTb4b4b4(Te6edf3objTb4b4b4, Ta5d6ff"Ta5d6ff__iter__Ta5d6ff"Tb4b4b4) Tff7b72and Tff7b72not Tffa657isinstanceTb4b4b4(Te6edf3objTb4b4b4, Tb4b4b4(Tffa657strTb4b4b4, Tffa657bytesTb4b4b4, Tffa657bytearrayTb4b4b4)Tb4b4b4)Tb4b4b4:
Tff7b72if Tffa657isinstanceTb4b4b4(Te6edf3objTb4b4b4, Tffa657dictTb4b4b4)Tb4b4b4:
Te6edf3contents Tff7b72= Tb4b4b4{Tb4b4b4}
Tff7b72for Te6edf3keyTb4b4b4, Te6edf3val Tff7b72in Te6edf3objTff7b72.Td2a8ffitemsTb4b4b4(Tb4b4b4)Tb4b4b4:
Te6edf3new_key Tff7b72= Te6edf3_recursion_helperTb4b4b4(Te6edf3keyTb4b4b4) Tff7b72if Te6edf3include_original_keys Tff7b72else Te6edf3key
Te6edf3new_val Tff7b72= Te6edf3_recursion_helperTb4b4b4(Te6edf3valTb4b4b4)
Te6edf3contentsTb4b4b4[Te6edf3new_keyTb4b4b4] Tff7b72= Te6edf3new_val
Tff7b72elseTb4b4b4:
Te6edf3contents Tff7b72= Tb4b4b4[Tb4b4b4]
Tff7b72for Te6edf3element Tff7b72in Te6edf3objTb4b4b4:
Te6edf3new_element Tff7b72= Te6edf3_recursion_helperTb4b4b4(Te6edf3elementTb4b4b4)
Te6edf3contentsTff7b72.Td2a8ffappendTb4b4b4(Te6edf3new_elementTb4b4b4)
T8b949e# Use the same class as the original.
Tff7b72return Te6edf3objTff7b72.Td2a8ff__class__Tb4b4b4(Te6edf3contentsTb4b4b4)
T8b949e# If it is not replaced and it is not an iterable, return it.
Tff7b72return Te6edf3obj
Tff7b72return Te6edf3_recursion_helperTb4b4b4(Te6edf3originalTb4b4b4)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────